package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.login.PhoneCityActivity;
import com.hzcx.app.simplechat.ui.login.event.PhoneCityEvent;
import com.hzcx.app.simplechat.ui.publicui.enums.SendCodeEnum;
import com.hzcx.app.simplechat.ui.setting.contract.UpdatePhoneContract;
import com.hzcx.app.simplechat.ui.setting.event.UpdatePhoneEvent;
import com.hzcx.app.simplechat.ui.setting.presenter.UpdatePhonePresenter;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;
import com.hzcx.app.simplechat.view.SendCodeTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class UpdatePhoneActivity extends BaseActivity<UpdatePhonePresenter> implements TextWatcher, UpdatePhoneContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_send_code)
    SendCodeTextView tvSendCode;
    @BindView(R.id.btn_commit)
    MainBlueConfirmButton btnCommit;
    @BindView(R.id.tv_phone_city_code)
    TextView tvPhoneCityCode;

    @Override
    public int getLayout() {
        return R.layout.activity_update_phone;
    }

    @Override
    public void initPresenter() {
        mPresenter=new UpdatePhonePresenter();
    }

    @Override
    public void initView() {
        etPhone.addTextChangedListener(this);
        etCode.addTextChangedListener(this);
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_send_code, R.id.tv_phone_city_code,R.id.btn_commit})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code: // 发送验证码
                if (EmptyUtils.isEmpty(etPhone.getText().toString())) {
                    showError("请输入手机号码");
                    return;
                }
                mPresenter.sendCode(this, etPhone.getText().toString(), SendCodeEnum.UPDATE_PHONE,
                        tvPhoneCityCode.getText().toString());
                break;

            case R.id.tv_phone_city_code: // 区号选择
                startActivity(new Intent(this, PhoneCityActivity.class));
                break;

            case R.id.btn_commit: // 提交修改
                mPresenter.updatePhone(this,etPhone.getText().toString(),etCode.getText().toString(),
                        tvPhoneCityCode.getText().toString());
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    /**
     * 检查手机号跟验证码是否输入完成
     *
     * @param charSequence
     * @param i
     * @param i1
     * @param i2
     */
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (EmptyUtils.isNotEmpty(etPhone.getText().toString()) && etCode.getText().toString().length() == 4) {
            btnCommit.setBtnEnabled(true);
        } else {
            btnCommit.setBtnEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    /**
     * 修改成功
     */
    @Override
    public void updateSuccess() {
        showError("修改成功");
        UserInfoBean bean= UserInfoUtil.getUserInfo();
        bean.setMobile(etPhone.getText().toString());
        bean.setArea_code(etCode.getText().toString());
        UserInfoUtil.saveUser(bean);
        EventBus.getDefault().post(new UpdatePhoneEvent());
        finish();
    }

    /**
     * 验证码发送成功
     */
    @Override
    public void sendCodeSuccess() {
        tvSendCode.start();
    }

    /**
     * 选择手机号地区返回
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void phoneCityChange(PhoneCityEvent event) {
        tvPhoneCityCode.setText(event.getBean().getPrefix());
    }

}
